Class and Objects


In [1]:
class Animal:
    age = None

In [11]:
class Dog(Animal):
    pass

In [4]:
german_shepherd =Dog()
# making a stance ie germa nshepherd is an instnce of dog

In [5]:
german_shepherd


Out[5]:
<__main__.Dog at 0x1d9e48007b8>

In [6]:
german_shepherd =Dog()
#it is a different instance as its menory locion shows

In [7]:
german_shepherd


Out[7]:
<__main__.Dog at 0x1d9e4800a90>

In [8]:
german_shepherd.age =22

In [9]:
german_shepherd.age


Out[9]:
22

In [12]:
class Dog(Animal):
    age=18

In [13]:
german_shepherd= Dog()

In [14]:
german_shepherd.age


Out[14]:
18

In [15]:
german_shepherd.age =21

In [16]:
german_shepherd.age


Out[16]:
21

In [17]:
tibetian_husky =Dog()

In [18]:
tibetian_husky.age


Out[18]:
18

In [26]:
class Animal:
    age = None
    def is_alive(self):
        return True
    def sound(self):
        return'cry'

In [ ]:


In [27]:
class Cat(Animal):
    def sound(self):
        return'meow'

In [ ]:


In [28]:
cat=Cat()

In [29]:
cat.is_alive()


Out[29]:
True

In [ ]:
cat.sound()

Case Study: Library Management


In [50]:
class Patron:
    firstname = None
    lastname = None
    address = None
    books =[]
    def __repr__(self):
        return'{} {}'.format(self.firstname,self.lastname)
    def has_book(self, bookid):
        return bookid in self.books

In [51]:
class Student(Patron):
    faculty =None
    year = None
    def __init__(self,firstname,lastname,faculty,year):
        self.firstname = firstname
        self.lastname = lastname
        self.faculty =faculty
        self.year = year

In [52]:
class Staff(Patron):
    department = None

In [53]:
class Book:
    bookid = None
    isbn = None
    name = None
    def __init__(self,isbn,name):
       
        self.isbn = isbn
        self.name = name
        self.bookid ='MYLIB'+':'+self.isbn
        #self must be 1st argumment
        #__ is called dunder
    def __repr__(self):
        return self.bookid

In [54]:
class Teacher(Patron):
    faculty = None
    def __init__(self,firstname,lastname,faculty):
        self.firstname = firstname
        self.lastname = lastname
        self.faculty =faculty

In [55]:
english = Book(isbn='abc-123454434',name='Learning English in 21 days')

In [56]:
math = Book(isbn='mth-12343435434',name='Learning math in 21 days')

In [57]:
nepali = Book(isbn='nep-65846324',name='Learning nepali in 21 days')

In [68]:
class Shelve:
    books =[]
    lended_book = []
    def is_book_available(self, bookid):
        return bookid in self.books
    def is_book_lendable(self,bookid):
        return bookid not in self.lended_books
    def borrow(self,bookid):
        
        a= input("enter book id to borrow")
        if(is_book_lendable(self,a)== True):
            lended_book.append(a)
        else:
            print('not lendable')

In [ ]:


In [61]:
my_library = Shelve()

In [17]:
# Shelve.is_book_available(my_library,'abc-123')
# we donot use this


Out[17]:
False

In [62]:
my_library.is_book_available('abc-123')


Out[62]:
False

In [63]:
my_library.books=[english,math,nepali]

In [64]:
my_library.books


Out[64]:
[MYLIB:abc-123454434, MYLIB:mth-12343435434, MYLIB:nep-65846324]

In [65]:
ram=Student('ram','sharma','bct','2071')

In [66]:
ram


Out[66]:
ram sharma

In [67]:
ram.has_book('assd')


Out[67]:
False

homework= to borrow book and add the borrowed book to the lendedbook list


In [72]:
Book


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-72-2e537a01d4f8> in <module>()
----> 1 Shelve.borrow()

TypeError: borrow() missing 2 required positional arguments: 'self' and 'bookid'

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: